/*
 * Copyright (C) 2010 Ryan Morton
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.games.rm.patternpuzzler;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RadioButton;

public class SettingsActivity extends Activity implements OnClickListener
{
	private LinearLayout mainSettingsLayout;
	private RadioButton soundFxOn;
	private RadioButton soundFxOff;
	private RadioButton buttons4;
	private RadioButton buttons5;
	private CheckBox speedIncrease;
	private Button clearHighScores;
	private Button resetSettings;
	
	private GameSettings mySettings;
	
	
	 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //set up the window properties
        Utilities.setupActivityWindow(this);
        
        //inflate the main layout
        this.setContentView(R.layout.settings);
        
        //run the init();
        this.init();
        
        mainSettingsLayout.startAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_in_main_menu));
    }
    
    /***
     * initialize the variables
     */
    private void init()
    {
    	mainSettingsLayout = (LinearLayout)this.findViewById(R.id.Main_Settings_Layout);
    	soundFxOn = (RadioButton)mainSettingsLayout.findViewById(R.id.Main_Settings_Fx_On);
    	soundFxOff = (RadioButton)mainSettingsLayout.findViewById(R.id.Main_Settings_Fx_Off);
    	buttons4 = (RadioButton)mainSettingsLayout.findViewById(R.id.Main_Settings_4_Buttons);
    	buttons5 = (RadioButton)mainSettingsLayout.findViewById(R.id.Main_Settings_5_Buttons);
    	clearHighScores = (Button)mainSettingsLayout.findViewById(R.id.Main_Settings_Clear_High_Scores_Button);
    	resetSettings = (Button)mainSettingsLayout.findViewById(R.id.Main_Settings_Reset_Settings_Button);
    	speedIncrease = (CheckBox)mainSettingsLayout.findViewById(R.id.Main_Settings_PatternSpeed_CheckBox);
    	clearHighScores.setOnClickListener(this);
    	resetSettings.setOnClickListener(this);
    	
    	//load the saved settings
    	mySettings = Utilities.loadSettings(this);
    	if(mySettings.isSoundOn())
    	{
    		soundFxOn.setChecked(true);
    	}
    	else
    	{
    		soundFxOff.setChecked(true);
    	}
    	
    	if(mySettings.getGameType()==Utilities.GAME_TYPE_4_BUTTON)
    	{
    		buttons4.setChecked(true);
    	}
    	else
    	{
    		buttons5.setChecked(true);
    	}
    	
    	speedIncrease.setChecked(mySettings.shouldSpeedIncrease());
    }

	@Override
	public void onClick(View v)
	{
		if(v==clearHighScores)
		{
			Utilities.clearHighScores(this);
		}
		else if(v==resetSettings)
		{
			soundFxOn.setChecked(true);
			buttons4.setChecked(true);
			speedIncrease.setChecked(false);
			Utilities.resetSettingsToDefault(this);
		}
	}
	
	@Override
	protected void onPause()
	{
		if(buttons4.isChecked())
		{
			Utilities.saveSettings(this,soundFxOn.isChecked(),Utilities.GAME_TYPE_4_BUTTON, speedIncrease.isChecked());
		}
		else
		{
			Utilities.saveSettings(this,soundFxOn.isChecked(),Utilities.GAME_TYPE_5_BUTTON, speedIncrease.isChecked());
		}
		super.onPause();
	}
}
